<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/1/4
 * Time: 10:23
 */
namespace app\admin\controller;
use think\Config;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use Common\Oss;
use think\Session;
class Discount extends Controller{
    public function discount(){
        $data = Request::instance()->post();
        // 商品列表
        $where                 = [];
        if (array_key_exists('search', $data) && !empty($data['search'])) {
            // 商品名称
            if (array_key_exists('name', $data['search']) && !empty($data['search']['name']))
                $where['p.name|s.name'] = ['LIKE', '%' . trim($data['search']['name']) . '%'];
        }
        $product['data'] = Db::name('discount')
            ->alias('a')
            ->join('product p','p.id = a.product_id','left')
            ->join('supplier s','s.id = p.supplier','left')
            ->order('a.id desc')
            ->where($where)
            ->field('a.*,p.name as name1,p.name_english as name_english1,s.name as name2,s.name_english as name_english2')
            ->limit(($data['pageNumber'] - 1) * $data['pageSize'], $data['pageSize'])
            ->select();
        foreach ($product['data'] as $key=>&$vo){
            if($vo['is_groom'] == 2){
                $vo['groom'] = '推荐';
            }else{
                $vo['groom'] = '不推荐';
            }
            if($vo['type']==1){
                $vo['type_name'] ='满减';
            }else{
                $vo['type_name'] ='满送';
            }
        }
        $product['pageNumber'] = $data['pageNumber'];
        if ($product['data'] === FALSE) return $result = json(Config::get('code.error'));
        // 商品总量
        $product['count'] = Db::name('discount')
            ->alias('a')
            ->join('product p','p.id = a.product_id','left')
            ->join('supplier s','s.id = p.supplier','left')
            ->order('a.id desc')
            ->where($where)
            ->field('a.*,p.name as name1,p.name_english as name_english1,s.name,s.name_english')
            ->count();
        return json($product);

    }


    public function authSupplier(){
        $id = input('param.id');
        $type = input('param.type');
        $res = Db::table('discount')->where('id',$id)->setField('is_groom',$type);
        if($res)
        {
            $result = Config::get('code.success');
        }
        else
        {
            $result = Config::get('code.error');
        }
        return json($result);

    }

}